package com.ls.kdlss.ui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.gm.common.utils.ActivityUtil;
import com.gm.lib.utils.GMToastUtil;
import com.ls.kdlss.R;
import com.ls.kdlss.ui.bangbang.BangFragment;
import com.ls.kdlss.ui.home.HomePagerFragment;
import com.ls.kdlss.ui.my.FindFragment;
import com.ls.kdlss.ui.mys.MysFragment;
import com.ls.kdlss.util.UserInfo;
import com.ls.kdlss.views.ForbidenScrollViewPager;
import com.ls.kdlss.views.TabSelectedButton;


/**
 * 主界面
 */
public class MainActivity extends AppCompatActivity {

    public static void lanuch(Context context) {
        ActivityUtil.startActivity(context, MainActivity.class);
    }

    private ForbidenScrollViewPager vpContainer;
    private MainPagerAdapter mMainPagerAdapter;
    private TabSelectedButton[] mTabViews; //底部的button
    private LinearLayout ll_bottom_tabs;

    private final String KEY_CURRENT_INDEX = "KEY_CURRENT_INDEX";
    private int statusCurrIndex = 0;// 当前页卡编号


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        initMainTabView();
        if (savedInstanceState != null) {
            statusCurrIndex = savedInstanceState.getInt(KEY_CURRENT_INDEX);
        }
        changeItem(statusCurrIndex);
        serUserids();


    }

    public void setUserTitle() {
    }

    /**
     * 给userid附值
     */
    public void serUserids() {

        SharedPreferences sharedPreferences = this.getSharedPreferences("Userid",
                Activity.MODE_PRIVATE);
        String userid = sharedPreferences.getString("id", "");
        UserInfo.userid = userid;
    }

    /**
     * 点击了某一个tab，显示他
     *
     * @param position
     */
    private void changeItem(int position) {
        //首页底部各个tab的点击
        unCheckAll();
        statusCurrIndex = position;
        mTabViews[position].setChecked(true);
        vpContainer.setCurrentItem(position, false);
    }


    /**
     * 添加底部tab
     *
     * @param position
     * @param textRes
     * @param imgRes
     */
    private void addTab(final int position, int textRes, int imgRes) {
        TabSelectedButton tabSelectedButton = TabSelectedButton.getView(MainActivity.this);
        tabSelectedButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
        tabSelectedButton.initData(textRes, imgRes);
        tabSelectedButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeItem(position);
            }
        });
        mTabViews[position] = tabSelectedButton;
        ll_bottom_tabs.addView(tabSelectedButton);
    }

    private void initMainTabView() {

        vpContainer = (ForbidenScrollViewPager) findViewById(R.id.vp_main);
        ll_bottom_tabs = (LinearLayout) findViewById(R.id.ll_bottom_tabs);
        vpContainer.setExpenseOnTouch(true);
        vpContainer.setForbidenScroll(true);
        vpContainer.setOffscreenPageLimit(4);
        mMainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
        vpContainer.setAdapter(mMainPagerAdapter);

        mTabViews = new TabSelectedButton[4];
        addTab(0, R.string.top_home, R.drawable.selector_tab_home);
        addTab(1, R.string.top_bang, R.drawable.selector_tab_bang);
        addTab(2, R.string.top_my, R.drawable.selector_tab_my);
        addTab(3, R.string.top_mys, R.drawable.selector_tab_mys);
    }


    private void unCheckAll() {
        for (TabSelectedButton button : mTabViews) {
            button.setChecked(false);
        }
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(KEY_CURRENT_INDEX, statusCurrIndex);
        super.onSaveInstanceState(outState);
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                GMToastUtil.showToast(R.string.press_once_again_exit_app);
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    /**
     * 设置程序字体大小不随系统字体大小的改变而改变
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    class MainPagerAdapter extends FragmentPagerAdapter {
        public MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;
            switch (i) {
                case 0:
                    //首页
                    fragment = HomePagerFragment.getInstance();
                    break;
                case 1:
                    //邦邦我
                    fragment = BangFragment.getInstance();
                    break;
                case 2:
                    //发现
                    fragment = FindFragment.getInstance();
                    break;
                case 3:
                    //我的
                    fragment = MysFragment.getInstance();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

}
